<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * File: models/ns_model.php
 * 
 * @package Nested_sets
 * @subpackage CI_NS
 */
include_once('nested_sets_model.php'); 

class Ns_model extends Nested_sets_model 
{

    /**
     * @note	setVars
     * @info	Initial vars needed to configure the Nested_sets model
     * @author	Dennis T. Kaplan
     * @access	public
     * @param	private 
     * @return	NULL
     */	
    private function setVars()
	{
        // This is the array of availabe ns tables in our db
        $this->nsTables = array('nsd','ns_db_meta','ns_dbTamer');
	}
    
    function __construct()
    {
        parent::Model();
        $this->setVars();
    }

    /**
     * @note	getTables
     * @info	Get available Tables
     * @author	Dennis T. Kaplan
     * @access	public
     * @param	NULL
     * @return	array
     */	
    public function getTables()
    {
    	return $this->nsTables;
    }
    

    /**
     * @note	setTable
     * @info	
     * @author	Dennis T. Kaplan
     * @access	public
     * @param	$table 
     * @return	
     */	
     public function setTable($table = 'nsd')
     {
         // The three parameters passed in here are:
         //  1) the table name
         //  2) the column name representing the "left value"
         //  3) the column name representing the "right value"
         
         $this->setControlParams($table,'lft','rgt');
         
         // Here we tell the nested sets model which column is used as the
         // primary key. In our example, it's the auto_incrementing "id"
         // column in the categories table.
         $this->setPrimaryKeyColumn('id');
         
         // Checking if table was set corectly
         $this->checkTable($table);
     }
    
    /**
    * @note	checkTable
    * @info	Checks if NS Table has been set
    * @author	Dennis T. Kaplan
    * @access	private
    * @param	 
    * @return	NULL
    */	
    private function checkTable()
    {
        if(!isset($this->table_name)){
            show_error('Nested Set Database Table not set');
        }elseif(!in_array($this->table_name, $this->nsTables)){
            show_error($this->table_name.' Is not a Nested Set Database Table');
        }
    }
    
    /**
    * Returns a form select / drop down.
    * Provide the form field name and this method will return an HTML snippet
    * suitable for using in a form
     * @param string $fieldname The form field name to use
     * @return string Html snippet appropriate for an HTML form
     */
    function getDataSelect($id, $selid = FALSE) 
    {
        $this->checkTable();
        $cats_handle = $this->getTreePreOrder($this->getNodeFromId($id));
        if(!empty($cats_handle['result_array'])) {

            $fieldname = $cats_handle['result_array'][0]['name'];
            $this->getTreeNext($cats_handle);
            $retVal = '<select name="'.$fieldname.">\n";
            if($selid === FALSE)$retVal .= '<option></option>'."\n";
            while($this->getTreeNext($cats_handle)) 
            {
                $sel = '';
                if($cats_handle['row']['id'] == $selid){
                    $sel = 'selected="selected"';
                }
                $retVal .= '<option '.$sel.' value="'.$cats_handle['row']['id'].'" \">"'.$cats_handle['row']['name']."</option>\n";
                
            }
        }
        
        $retVal .= "</select>\n";
        return $retVal;
    }
    
    /**
     * Returns a form select / drop down.
     * Provide the form field name and this method will return an HTML snippet
     * suitable for using in a form
     * @param string $fieldname The form field name to use
     * @return string Html snippet appropriate for an HTML form
     */
    function getAllDataSelect($fieldname) 
    {
        $this->checkTable();
        $retVal = "<select name=\"$fieldname\">\n";
        $cats_handle = $this->getTreePreOrder($this->getRoot());
        
        if(!empty($cats_handle['result_array'])) {
            while($this->getTreeNext($cats_handle)) 
            {
                // get indent value
                $indent = (str_repeat("&nbsp;", $this->getTreeLevel($cats_handle)*2));
                $retVal .= "<option value=\""  .$cats_handle['row']['id'] . "\">$indent".$cats_handle['row']['name']."</option>\n";
                
            }
        }
        
        $retVal .= "</select>\n";
        return $retVal;
    }
    
    /**
     * Overrides the nested_sets_model method to define the dataset field to
     * display in the tree by passing array("categoryname") to the getSubTree 
     * method
     * @return string html snippet representing the tree
     */
    function getTreeAsHTML()
    { 
        $this->checkTable();
        return $this->getSubTreeAsHTML($this->getRoot(), array('name','id'));
    }
}
?>
